package com.ys.shop.dao;

import com.ys.shop.db.IDB;
import com.ys.shop.db.MySqlDBImp;
import com.ys.shop.vo.Product;
import com.ys.shop.vo.VO;

import javax.naming.ldap.PagedResultsControl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodDAOImp implements IDAO{

    @Override
    public boolean insert(VO vo) throws SQLException {
        return false;
    }

    @Override
    public List<VO> getAll() throws SQLException {

        PreparedStatement preparedStatement=null;
        ResultSet rs=null;
        List<VO> list=new ArrayList<VO>();
        IDB db=new MySqlDBImp();
        Connection conn=null;
        try {
        conn=db.getConn();
        String sql="select * from Easybuy_Product";
        preparedStatement=conn.prepareStatement(sql);
        rs=preparedStatement.executeQuery();
        while (rs.next()){
            Product product=new Product();
            product.setEp_id(rs.getInt(1));
            product.setEp_name(rs.getString(2));
            product.setEp_description(rs.getString(3));
            product.setEp_price(rs.getFloat(4));
            product.setEp_stock(rs.getInt(5));
            product.setEpc_id(rs.getInt(6));
            product.setEpc_child_id(rs.getInt(7));
            product.setEp_file_name(rs.getString(8));
            list.add(product);
        }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        rs.close();
        preparedStatement.close();
        conn.close();
        return list;
    }

    @Override
    public boolean deleteByID(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean update(VO vo) throws SQLException {
        return false;
    }

    @Override
    public VO getByID(VO vo){
//        Product product= (Product) vo;
//        Connection conn=null;//连接对象
//        ResultSet rs=null;//结果集
//        PreparedStatement preparedStatement=null;//操作数据库
//        IDB db=new MySqlDBImp();
//        try {
//            conn=db.getConn();
//        String sql="SELECT * from easybuy_product WHERE ep_id=?";//SQL语句
//        Product product= (Product) vo;
//        preparedStatement.setInt(1,product.getEp_id());
//        preparedStatement=conn.prepareStatement(sql);
//        rs=preparedStatement.executeQuery();
//        Product p=new Product();
//        while (rs.next()){
//            p.setEp_id(rs.getInt(1));
//            p.setEp_name(rs.getString(2));
//            p.setEp_description(rs.getString(3));
//            p.setEp_price(rs.getFloat(4));
//            p.setEp_stock(rs.getInt(5));
//            p.setEpc_id(rs.getInt(6));
//            p.setEpc_child_id(rs.getInt(7));
//            p.setEp_file_name(rs.getString(8));
//        }
//        rs.close();
//        preparedStatement.close();
//        db.closegetConn();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
        return null;
    }
    public Product getGoodByID(int id){
        Connection conn=null;//连接对象
        ResultSet rs=null;//结果集
        PreparedStatement preparedStatement=null;//操作数据库
        Product product=new Product();//定义一个商品实体类型，用于存储数据
        IDB db1=new MySqlDBImp();
        MySqlDBImp db=new MySqlDBImp();
        try {
            conn=db.getConn();//1、获得数据库连接    2、获取工具PreparedStatement
            String sql="SELECT * from easybuy_product WHERE ep_id=?";//SQL语句
            //给？注入值
            preparedStatement=conn.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            rs=preparedStatement.executeQuery();//3、操作数据库
            if(rs.next()){//从结果集中将仅有的一行数据注入到实体类对象中
                product.setEp_id(rs.getInt("Ep_id"));
                product.setEp_name(rs.getString("Ep_name"));
                product.setEp_description(rs.getString("Ep_description"));
                product.setEp_price(rs.getFloat("Ep_price"));
                product.setEp_stock(rs.getInt("Ep_stock"));
                product.setEpc_id(rs.getInt("Epc_id"));
                product.setEpc_child_id(rs.getInt("Epc_child_id"));
                product.setEp_file_name(rs.getString("Ep_file_name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {//3、释放资源
                rs.close();
                preparedStatement.close();
                db.closegetConn();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return product;//4、返回实体对象
    }

    public static void main(String[] args) {
        GoodDAOImp daoImp=new GoodDAOImp();
        System.out.println(daoImp.getGoodByID(3).getEp_name());
    }
}
